package com.papa91.wrapper;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

/**
 * Created by xiet on 2015/7/30.
 */
public class KeyboardListenLayout extends RelativeLayout {
	private String TAG="KeyboardListenLayout";

	public KeyboardListenLayout(final Context context, final AttributeSet attrs) {
		super(context, attrs);
	}

	public KeyboardListenLayout(Context context) {
		super(context);
	}

	private OnSoftKeyboardListener onSoftKeyboardListener;
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		Log.e(TAG, "oldh="+oldh+" h="+h+"");
		super.onSizeChanged(w, h, oldw, oldh);
	}

	@Override
	protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
			final int newSpec = MeasureSpec.getSize(heightMeasureSpec);
			final int oldSpec = getMeasuredHeight();
			int w = this.getWidth();
			int height = this.getHeight();
			// If layout became smaller, that means something forced it to resize. Probably soft keyboard :)
			Log.e(TAG, "height="+height+" w="+w+"");
		if (onSoftKeyboardListener != null) {
			if (oldSpec > newSpec){
				onSoftKeyboardListener.onShown();
			} else {
				onSoftKeyboardListener.onHidden();
			}
		}
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	public final void setOnSoftKeyboardListener(final OnSoftKeyboardListener listener) {
		this.onSoftKeyboardListener = listener;
	}

	// Simplest possible listener :)
	public interface OnSoftKeyboardListener {
		public void onShown();
		public void onHidden();
	}
}